package com.augurit.common.system.web.org;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.utils.SpringContextHolder;

import com.augurit.common.base.action.ExtCrudActionSupport;
import com.augurit.common.base.extmodel.base.BaseTreeNode;
import com.augurit.common.base.extmodel.inner.AppTreeNode;
import com.augurit.common.base.extmodel.inner.MultiEntityTreeNode;
import com.augurit.common.base.listener.AosInitListener;
import com.augurit.common.entity.AcApplication;
import com.augurit.common.entity.AcModule;
import com.augurit.common.system.convert.AcApplicationConverter;
import com.augurit.common.system.convert.AcModuleConverter;
import com.augurit.common.system.service.org.IAcApplicationService;
import com.augurit.common.system.service.org.IAcModuleService;
import com.augurit.common.system.service.org.IAcServerService;
import com.augurit.common.system.service.org.IOmUserService;
import com.augurit.common.system.util.OrgUtils;
import com.augurit.common.system.util.WebContextUtils;
import com.augurit.common.system.web.org.form.AcApplicationForm;
import com.augurit.common.system.web.org.form.AcModuleForm;
import com.augurit.common.system.web.org.form.AcServerForm;
import com.augurit.common.util.ExtUtils;

@SuppressWarnings("serial")
@Results({
	@Result(name = ExtCrudActionSupport.RELOAD, location = "ac-app.action", type = "redirect"),
	@Result(name = ExtCrudActionSupport.SUCCESS, location = "/common/system/org/ac-application.jsp")
})
public class AcApplicationAction extends ExtCrudActionSupport<AcApplicationForm> {

	@Autowired
	private IOmUserService omUserService;
	
	@Autowired
	private IAcServerService acServerService;
	
	@Autowired
	private IAcApplicationService acApplicationService;
	
	@Autowired
	private IAcModuleService acModuleService;
	
	// 属性
	private String nodeId;	//应用管理树结点ID，由 Entity类名 + "#" + Entity对象ID 组成 
	private AcApplicationForm form;
	private Long id;
	private Long[] appIds;
	private Page<AcApplicationForm> page = new Page<AcApplicationForm>(15);


	/**
	 * 应用列表查询
	 */
	@Override
	public String list() throws Exception {
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);

		acApplicationService.getAllApps(page, form);
		
		this.extRenderGridJson(page);
		return null;
	}	

	/**
	 * 新增或编辑应用页面
	 */
	@Override
	public String input() throws Exception {
		this.extRenderFormJson(form);
		return null;
	}
	
	/**
	 * 保存应用信息
	 */
	@Override
	public String save() throws Exception {
		acApplicationService.saveAppCascade(form);
		
		//同步更新应用列表
		AosInitListener.loadAcApplicationList(this.getApplication(), 
				SpringContextHolder.getApplicationContext());
		
		this.extRenderSuccess();
		return null;
	}
	
	/**
	 * 删除一个或多个应用
	 */
	@Override
	public String delete() throws Exception {
		acApplicationService.deleteAppCascade(appIds);
		
		//同步更新应用列表
		AosInitListener.loadAcApplicationList(this.getApplication(), 
				SpringContextHolder.getApplicationContext());
		
		this.extRenderSuccess();
		return null;
	}
	
	/**
	 * 获取菜单树指定树节点的子节点
	 */
	public String getAppTree() throws Exception {
		MultiEntityTreeNode node = AppTreeNode.build(nodeId);
		List<AppTreeNode> result = getChildren(node);
		this.extRenderListJson(result, false);
		return null;
	}

	/**
	 * 根据指定树节点获取子对象列表
	 */
	private List<AppTreeNode> getChildren(MultiEntityTreeNode node){
		List<AppTreeNode> result = null;
		
		if(node != null){
			result = new ArrayList();
			
			// 获取所有应用
			if(MultiEntityTreeNode.isRootNode(node.getId())){
				List<AcApplicationForm> list = acApplicationService.getAllApps();
				result = AcApplicationConverter.convertAppToTreeNode(list);
			}
			// 获取子模块列表
			else{
				if(node.getEntityName().equals(AcApplication.class.getSimpleName())){
					List<AcModuleForm> list = acApplicationService.getModules(node.getEntityId());
					result = AcApplicationConverter.convertModuleToTreeNode(list);
				}
				else if(node.getEntityName().equals(AcModule.class.getSimpleName())){
					AcModuleForm form = acModuleService.getModuleRelatedElements(node.getEntityId(), WebContextUtils.getSysCodeBuffer(this.getApplication()));
					result = AcModuleConverter.convertChildListToTreeNodeList(form);
				}
			}
		}
		
		return result;
	}

	/**
	 * 一次性获得简单check树（只有功能树节点上含有check选项）
	 * 注意：只有系统管理员才有开通/禁止特定功能权限
	 */
	public String getSelFuncCheckTree() throws Exception{

		//系统管理员
		if(omUserService.isSystemAdmin(this.getLoginUserId())){
			List<AcApplicationForm> appList = new ArrayList();
	
			List<AcApplicationForm> activeApps = WebContextUtils.getAcApplicationList(
					acServerService, acApplicationService, true, this.getApplication());
			
			if(activeApps != null && activeApps.size() > 0){
				for(AcApplicationForm activeApp : activeApps)
					appList.add(acApplicationService.getTreeByApp(activeApp, false));
			}
			
			List<BaseTreeNode> list = AcApplicationConverter.OneTimeConvertAllAppsToSimpleSelTree(appList);
			this.extRenderListJson(list, false);
		}
		else{
			List<BaseTreeNode> list = new ArrayList();
			list.add(OrgUtils.getNoPrivTipTreeNode());
			this.extRenderListJson(list, false);
		}
		
	    return null;
	}	
	
	
	public AcApplicationForm getModel() {
		return form;
	}
	
	@Override
	protected void prepareModel() throws Exception {
		if(id != null){
			form = acApplicationService.get(id);
		}else{
			form = new AcApplicationForm();
		}
	}
	
	// 属性getter和setter方法
	public String getNodeId() {
		return nodeId;
	}

	public void setNodeId(String nodeId) {
		this.nodeId = nodeId;
	}

	public AcApplicationForm getForm() {
		return form;
	}

	public void setForm(AcApplicationForm form) {
		this.form = form;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	public Long[] getAppIds() {
		return appIds;
	}

	public void setAppIds(Long[] appIds) {
		this.appIds = appIds;
	}

	public Page<AcApplicationForm> getPage() {
		return page;
	}

	public void setPage(Page<AcApplicationForm> page) {
		this.page = page;
	}
}
